package com.itluma.myshopping.servlet;

import com.itluma.myshopping.entity.Product;
import com.itluma.myshopping.pojo.PageBean;
import com.itluma.myshopping.service.ProductService;
import com.itluma.myshopping.service.impl.ProductServiceImpl;
import com.itluma.myshopping.utils.StrUtils;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 飞鸟
 * @create 2021-03-15 10:31
 * /模块名称？action=操作
 *
 * /product?action=add
 */
@WebServlet(value = "/product")
public class ProductServlet extends BaseServlet {

    public ProductServlet(){
        System.out.println("ProductServlet对象被构造成功了！");
    }

    /**
     *从header.jsp中的超链接条状
     * @author 小陆
     * @date 2021/3/15
     * @param
     * @param
     * @return java.lang.String
     */
    @SneakyThrows
    public void show(HttpServletRequest req, HttpServletResponse resp) {
        String tid = req.getParameter("tid");//获取商品类型id
        String recpage = req.getParameter("page");//获取页数
        //String keyWord = req.getParameter("keyWord");//获取搜索关键字
        Integer page = 1;//默认展示第一页
        if (StrUtils.empty(recpage)) {//判断是否为空
            page = Integer.valueOf(recpage);//赋值
        }
        Integer pagesize = 8;//每页展示十条
        System.out.println(tid);
        ProductService productService = new ProductServiceImpl();//创建Service接口实例
        PageBean<Product> ProductList = productService.showAll(Integer.valueOf(tid),page ,pagesize);//调用showAll方法获取返回的对象

        if (ProductList != null) {
            req.setAttribute("pageBean",ProductList);
            req.getRequestDispatcher("/goodsList.jsp").forward(req,resp);
        }
    }

    public String add(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Product--add()");
        request.setAttribute("list", "数据");
        return "forward:/xxx";
    }

    public String update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Product--update()");
        return "value";
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("Product--delete()");
    }
}

